/**
 * Created with JetBrains WebStorm.
 * User: peternguyen
 * Date: 8/4/13
 * Time: 11:15 AM
 * To change this template use File | Settings | File Templates.
 */

describe('MoveWhenController', function() {
    var controller;
    beforeEach(function(){
        App.MoveWhenController.reopen({
            init:function(){
            }
        });

        controller = App.MoveWhenController.create({
            controllers: Ember.Object.create({
                alert: Ember.Object.create({
                    showAlert: function() {
                    }
                })
            })
        });
    });

    describe('continueAction', function() {
        it('should call webservice for billpayment', function() {
            controller.set('transfer', App.BillPayment.create());

            spyOn(controller, 'showLoading');
            spyOn(controller, 'acceptTransferCallback');
            spyOn(App.api_transfer, 'acceptTransfer').andCallFake(function(transfer, callback) {
                callback({}, {});
            });

            controller.continueAction();

            expect(controller.showLoading).toHaveBeenCalled();
            expect(App.api_transfer.acceptTransfer).toHaveBeenCalled();
            expect(controller.acceptTransferCallback).toHaveBeenCalled();

        });


    });

    describe('acceptTransferCallback', function() {
        it('show confirm if successful', function(){
            var transferObject = Ember.Object.create({
                isCrossTransfer: false
            });

            controller.set('transfer', transferObject);

            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.acceptTransferCallback({}, {});

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.transitionTo).toHaveBeenCalledWith('move.preConfirm');

        });
        it('update fx details and show confirm if transfer is cross currency and successful', function(){
            var transferObject = Ember.Object.create({
                isCrossTransfer: true,
                toAccount: {
                    currencyCode: 'IDR'
                }
            });

            controller.set('transfer', transferObject);

            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.acceptTransferCallback({
                exchangeRate: '1.1',
                debitAmount: 100.1
            }, null);

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(transferObject.getProperties('indicativeExchangeRate', 'equivalentAmount')).toEqual({
                indicativeExchangeRate:'1.1',
                equivalentAmount: 100.1
            });
            expect(controller.transitionTo).toHaveBeenCalledWith('move.preConfirm');

        });

        it('cashAdvance , bank charges', function(){
            var transferObject = Ember.Object.create({
                isCashAdvance: true,
                bankCharges:'10',
                finCharges:'20',
                cashAvail:100
            });

            controller.set('transfer', transferObject);

            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');

            controller.acceptTransferCallback({
                bankChargesStmt:'10',
                finChargesStmt:'20',
                cashAvail:100
            }, null);

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(transferObject.getProperties('bankCharges', 'finCharges', 'cashAvail')).toEqual({
                bankCharges:'10',
                finCharges:'20',
                cashAvail:100
            });
            expect(controller.transitionTo).toHaveBeenCalledWith('move.preConfirm');

        });

//        it('update bank charges,finance charges and available cashLimit -OAT CC', function(){
//            var transferObject = Ember.Object.create({
//                isCashAdvance: true,
//                bankCharges:10,
//                financeCharges:20,
//                availableCashLimit:100
//            });
//
//            controller.set('transfer', transferObject);
//
//            spyOn(controller, 'hasError').andReturn(false);
//            spyOn(controller, 'hideLoading');
//            spyOn(controller, 'transitionTo');
//
//            controller.acceptTransferCallback({
//                bankCharges:10,
//                financeCharges:20,
//                availableCashLimit:100
//            }, null);
//
//            expect(controller.hideLoading).toHaveBeenCalled();
//            expect(transferObject.getProperties('bankCharges', 'financeCharges', 'availableCashLimit')).toEqual({
//                bankCharges:10,
//                financeCharges:20,
//                availableCashLimit:100
//            });
//            expect(controller.transitionTo).toHaveBeenCalledWith('move.preConfirm');
//
//        });

        it('show error and redirect to move home if there is error', function(){
            spyOn(controller, 'hasError').andReturn(true);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'transitionTo');
            spyOn(controller.get('controllers').get('alert'), 'showAlert').andCallFake(function(mesg, callback){
                callback();
            });

            controller.acceptTransferCallback({}, BreezeException.create());

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.transitionTo).toHaveBeenCalledWith('move.home');

        });
    });

});